package de.jppietsch.error;


import javax.swing.*;
import java.util.HashMap;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;


public class GUIHandler extends Handler {

    private static final HashMap map = new HashMap();

    static {
        map.put(Level.SEVERE, new Integer(JOptionPane.ERROR_MESSAGE));
        map.put(Level.WARNING, new Integer(JOptionPane.WARNING_MESSAGE));
        map.put(Level.INFO, new Integer(JOptionPane.INFORMATION_MESSAGE));
        map.put(Level.CONFIG, new Integer(JOptionPane.INFORMATION_MESSAGE));
        map.put(Level.FINE, new Integer(JOptionPane.INFORMATION_MESSAGE));
        map.put(Level.FINER, new Integer(JOptionPane.INFORMATION_MESSAGE));
        map.put(Level.FINEST, new Integer(JOptionPane.INFORMATION_MESSAGE));
    }

    public GUIHandler() {
        setLevel(Level.WARNING);
    }

    public void close() {
        flush();
    }

    public void flush() {
    }

    public void publish(final LogRecord aRecord) {
        final Level level = aRecord.getLevel();

        if (getLevel().intValue() <= level.intValue()) {
            final Integer type = (Integer) map.get(level);
            final int msgType = (type != null)
                    ? type.intValue()
                    : JOptionPane.ERROR_MESSAGE;

            JOptionPane.showMessageDialog(null, aRecord.getMessage(),
                    level.toString(), msgType);
        }
    }

}